<?php
namespace app\admin\controller;

use app\common\controller\Admin ; 

class Option extends Admin
{
    


    //控制器初始化
    public function _initialize()
    {
        parent::_initialize();

        $this->model = model('Option');
    }
    /**
     * 组织管理 
     */
    public function index()
    {
        $map = array();

        $pid = input('pid/d', 0 );


        $map['pid'] = $pid ;
  
        $list = $this->model->field(true)->where($map)->order( 'pid asc , id asc ')->select();
        $column_0 = $this->model->where('pid = 0')->order( 'pid asc , id asc ')->column('name','id');
        // // ->paginate(20,false,['query' => request()->param()]);
        // 

        $data= array(
            'list' => $list,
            'column_0' => $column_0,
            'pid' => $pid,
            );
         $this->assign($data);
         $this->setMeta('超管配置|option管理');
         return view();
    }

    /**
     * [add description]
     */
    public function add(){
        if(IS_AJAX){
            $data = input('post.');
// dump($data);
            if($data){
                $rs = $this->model->isUpdate(false)->validate('option.add')->save($data);
                if (false === $rs) {
                    return $this->error($this->model->getError() );
                } else {
                    $this->model->cache_clear();
                    return $this->success('添加成功');
                }   
            }else{
                return $this->error( '没有post数据' );
            }
        }else{

            $pid = input('pid/d' , 0 );
            $max_id = $this->model->where('pid = '.$pid )->max('id');
            if($max_id == 0 ){ $max_id = $pid * 100 ;}

            $info = array();
            $info['id'] = $max_id + 1 ;
            $info['pid'] = $pid;
            $info['name'] = '';

            $column_0 = $this->model->where('pid = 0')->order( 'pid asc , id asc ')->column('name','id');

            $this->assign('info' , $info );
            $this->assign('column_0' , $column_0 );

            $this->setMeta('添加');
            return view('edit');
        }
    }

    public function upt(){
        if($this->request->isAjax() ){
            $data = input('post.');
// dump($data);
            if($data){
                $rs = $this->model->isUpdate(true)->validate('option.upt')->save($data);
                if (false === $rs) {
                    return $this->error($this->model->getError() );
                } else {
// echo $this->model->getLastSql();
                    $this->model->cache_clear();
                    return $this->success('修改成功');
                }   
            }else{
                return $this->error( '没有post数据' );
            }
        }else{

            $id = input('id/d');
            $info = $this->model->get($id);

            if(!$info){$this->error('参数错误！！！');}
            $column_0 = $this->model->where('pid = 0')->order( 'pid asc , id asc ')->column('name','id');

            $this->assign('info' , $info );
            $this->assign('column_0' , $column_0 );

            $this->setMeta('修改');
            return $this->fetch('edit');
        }

    }





    /**
     * 删除配置  一个删除 和 多个删除
     *  
     */
    public function del() {
        $id = array_unique((array) input('id/a', 0));

        if (empty($id)) {
            return $this->error('请选择要操作的数据!');
        }

        $map = array('id' => array('in', $id));
// echo $this->model->getLastSql();

        if (false !== $this->model->where($map)->field('*')->delete() ) {
            $this->model->cache_clear();
            return $this->success('删除成功！' );   //$msg = '', $url = null, $data = '', $wait = 3, array $header = []
        } else {
            return $this->error('删除失败！' );    // $msg = '', $url = null, $data = '', $wait = 3, array $header = []
        }
    }



}


